package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.home.activity.ArticleDetailActivity
import com.stars.home.activity.MainActivity
import com.stars.home.model.LoginBean
import com.stars.home.repository.OneLoginRepository
import com.stars.home.utils.WeChatUtil
import com.stars.home.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch


open class OneLoginVModel(application: Application) : BViewModel<OneLoginRepository>(application) {

    var isRead= MutableLiveData<Int>(0)//0未勾选 1已勾选

    fun toDetails(type:Int?){
        UiSwitch.bundle(repository().context, ArticleDetailActivity::class.java,
            B().putString("type",type.toString()).ok())
    }

    fun login(code: String) {
        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.weChatLogin(code) }
            if (it.ok()) {
                var bean=it.data
                MMKV.defaultMMKV().putString("token", it.data?.token)

                UiSwitch.single(repository().context, MainActivity::class.java)
                (repository().context as Activity).finish()

//                if(bean?.memberInfo?.type==0&&bean?.memberInfo?.status==0){
//
//                    UiSwitch.bundle(repository().context, CertificationActivity::class.java, B().putString("type","1").ok())
//                    (repository().context as Activity).finish()
//                }else{
//                    UiSwitch.single(repository().context, MainActivity::class.java)
//                    (repository().context as Activity).finish()
//                }

            }
        }
    }

    fun wxLogin() {

        if(isRead.value==0){
            toast("请勾选")
            return
        }
        //微信登录
        WeChatUtil.setWXLoigin(repository().context)

    }


    fun simLogin(mobile: String, type: String) {
        repository().simLogin(mobile, type)
    }

    fun oneLogin(code: String) {

        repository().rxLreq(NetEngine.service.jiguangLogin(code))
            .doOnNext() { res ->
                if (res.ok(true)) {
                    //保存token

                    var bean: LoginBean = res.data() as LoginBean;

                    MMKV.defaultMMKV().putString("token", bean.token)
                    UiSwitch.single(repository().context, MainActivity::class.java)
                    (repository().context as Activity).finish()
                } else {
                    if (res.code() == 0) {

                        var bean: LoginBean = res.data() as LoginBean

//                            IdentityDialogUtils.shoChooseIdentity(repository().context!!, cl!!, object : ChoiceData {
//
//                                override fun onRestuse(type: String?) {

//                                    simLogin(bean.mobile, type!!);

//                                }
//
//                            })


//                            var bean: LoginBean = res.data() as LoginBean;
//                            val items = arrayOf("司机", "货主")
//                            val builder = QMUIDialog.CheckableDialogBuilder(repository().context)
//                            builder.setTitle("选择类型去注册")
//                                    .setCheckedIndex(0)
//                                    .addItems(items) { dialog, which -> builder.checkedIndex = which }
//                                    .addAction("取消") { dialog, index ->
//                                        dialog.dismiss()
//                                    }
//                                    .addAction("确定") { dialog, index ->
//                                        var type = if (builder.checkedIndex == 0) "1" else "2"
//                                        simLogin(bean.mobile, type);
//                                        dialog.dismiss()
//                                    }
//                                    .show()


//

                    }
                }
            }
            .subscribe()
    }

    override fun createRepository(): OneLoginRepository {
        return OneLoginRepository()
    }

}
